package com.renzhichu.app.polyv.vod;

import android.support.annotation.Nullable;

import com.easefun.polyvsdk.log.PolyvCommonLog;
import com.easefun.polyvsdk.player.IPolyvRNMediaController;
import com.easefun.polyvsdk.screencast.utils.PolyvToastUtil;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.renzhichu.app.polyv.vod.protocol.IPolyvRNVideoPlayer;
import com.renzhichu.app.polyv.vod.protocol.IPolyvVodOrientotion;
import com.renzhichu.app.polyv.vod.view.PolyvRNVodPlayer;

import java.util.Map;

import static com.renzhichu.app.polyv.vod.PolyvRNConstants.RN_MARQUEE;

/**
 * @author df
 * @create 2019/2/20
 * @Describe
 */
public class PolyvVodPlayer extends ViewGroupManager<PolyvRNVodPlayer> implements IPolyvRNVideoPlayer {

    private static final String TAG = "PolyvVodPlayer";
    private static final String EVENT_NAME_VOD = "onPlayVodEvent";
    private static final String POLYV_VOD_PLAYER = "PolyvVodPlayer";
    private static final int POLYVVODPLAYER_EVENT_UPDATEVID = 808;
    private static final int POLYVVODPLAYER_EVENT_START_OR_PAUSE = 809;
    private static final int POLYVVODPLAYER_EVENT_PLAY = 810;
    private static final int POLYVVODPLAYER_EVENT_PAUSE = 811;
    private static final int POLYVVODPLAYER_EVENT_RELEASE = 812;
    private static final int POLYVVODPLAYER_EVENT_FULLSCREEN = 813;
    private static final int POLYVVODPLAYER_EVENT_INIT_PLAY = 814;
    private static final int POLYVVODPLAYER_EVENT_ORIENTATION = 815;

    private ThemedReactContext reactContext;

    @Override
    public String getName() {
        return POLYV_VOD_PLAYER;
    }

    @Override
    protected PolyvRNVodPlayer createViewInstance(ThemedReactContext reactContext) {
        this.reactContext = reactContext;
        return new PolyvRNVodPlayer(reactContext.getCurrentActivity());
    }

    @Override
    public void onDropViewInstance(PolyvRNVodPlayer view) {
        super.onDropViewInstance(view);
//        if(view != null){
//            view.onDestroy();
//        }
    }

    @javax.annotation.Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        Map<String, Integer> map = MapBuilder.of("updateVid", POLYVVODPLAYER_EVENT_UPDATEVID,
                "startOrPause", POLYVVODPLAYER_EVENT_START_OR_PAUSE,
                "play", POLYVVODPLAYER_EVENT_PLAY,
                "pause", POLYVVODPLAYER_EVENT_PAUSE,
                "setFullScreen", POLYVVODPLAYER_EVENT_FULLSCREEN,
                "release", POLYVVODPLAYER_EVENT_RELEASE,
                "initPlay", POLYVVODPLAYER_EVENT_INIT_PLAY
        );
        map.put("orientation", POLYVVODPLAYER_EVENT_ORIENTATION);
        return map;
    }

    public void onReceiveNativeVodEvent(PolyvRNVodPlayer view, String key, boolean value) {
        WritableMap event = Arguments.createMap();
        event.putString("key", key);
        event.putBoolean("value", value);
        this.reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(view.getId(), EVENT_NAME_VOD, event);
    }

    /**
     * 自定义事件
     *
     * @return
     */
    @Nullable
    @Override
    public Map getExportedCustomDirectEventTypeConstants() {
        Map map = MapBuilder.of(EVENT_NAME_VOD, MapBuilder.of("registrationName", EVENT_NAME_VOD));
        return map;
    }

    private PolyvVodOrientoinListener orientoinListener;

    @Override
    public void receiveCommand(PolyvRNVodPlayer root, int commandId, @javax.annotation.Nullable ReadableArray args) {
//        Toast.makeText(root.getContext(), "收到RN层的任务通知...:" + commandId, Toast.LENGTH_LONG).show();
        switch (commandId) {
            case POLYVVODPLAYER_EVENT_UPDATEVID:
                if (args != null) {
                    String vid = args.getString(0);
//                    root.setVid(vid.trim());
                    root.updateVid(vid.trim());
                }
                break;
            case POLYVVODPLAYER_EVENT_INIT_PLAY:
                System.out.println(POLYV_VOD_PLAYER + "startPlay");
                root.startPlay();
                reactContext.addLifecycleEventListener(new LifecycleEventListener() {
                    @Override
                    public void onHostResume() {
                        System.out.println(POLYV_VOD_PLAYER + "onHostResume");
//                        root.onResume();
                        root.vedioStart();
                    }

                    @Override
                    public void onHostPause() {
                        System.out.println(POLYV_VOD_PLAYER + "onHostPause");
//                        root.playOrPause();
                        root.vedioPause();
                    }

                    @Override
                    public void onHostDestroy() {
                        System.out.println(POLYV_VOD_PLAYER + "onHostDestroy");
//                        root.onDestory();
                    }
                });
                if (orientoinListener == null) {
                    orientoinListener = new PolyvVodOrientoinListener(reactContext.getCurrentActivity(), new IPolyvVodOrientotion() {
                        @Override
                        public void changeToPortrait() {
                            root.setPlayerFullScreen(false);
                        }

                        @Override
                        public void changeToLandscape() {
                            root.setPlayerFullScreen(true);
                        }
                    });
                    orientoinListener.enable();
                }
                break;
            case POLYVVODPLAYER_EVENT_START_OR_PAUSE:
                if (root.isPlaying()) {
                    root.vedioPause();
                } else {
                    root.vedioStart();
                }
                break;
            case POLYVVODPLAYER_EVENT_PLAY:
                if (!root.isPlaying()) {
                    root.vedioStart();
                }
                break;
            case POLYVVODPLAYER_EVENT_PAUSE:
                if (root.isPlaying()) {
                    root.vedioPause();
                }
                break;
            case POLYVVODPLAYER_EVENT_RELEASE:
                root.onDestroy();
                if (orientoinListener != null) {
                    orientoinListener.disable();
                    orientoinListener = null;
                }
                break;
            case POLYVVODPLAYER_EVENT_FULLSCREEN:
                if (args != null) {
                    Boolean fullScreen = args.getBoolean(0);
                    root.setPlayerFullScreen(fullScreen);
                }
                break;
            case POLYVVODPLAYER_EVENT_ORIENTATION:
                if (args != null) {
                    Boolean orientation = args.getBoolean(0);
                    System.out.println(TAG + "orientoinListener" + orientation);
                    if (orientoinListener != null) {
                        if (orientation) {
                            orientoinListener.enable();
                        } else {
                            orientoinListener.disable();
                        }
                    }
                }
                break;
        }
    }

    @ReactProp(name = PolyvRNConstants.RN_VID)
    @Override
    public void setVid(PolyvRNVodPlayer player, @Nullable String vid) {
        PolyvCommonLog.d(POLYV_VOD_PLAYER, "set vid :" + vid);
        player.play(vid, 1, false, false);
    }

    @ReactProp(name = PolyvRNConstants.RN_PLAY_PARAMETERS)
    @Override
    public void setVid(PolyvRNVodPlayer player, ReadableMap readableMap) {
        try {
            boolean isAutoStart = false;
            boolean isFullScreen = false;
            boolean showControl = true;
            boolean loopPlay = false;
            boolean isCanDrag = true;
//            if(!readableMap.hasKey(PolyvRNConstants.RN_VID)){
////                PolyvToastUtil.show(player.getContext(),"vid is error ");
//                return;
//            }

            if (readableMap.hasKey(PolyvRNConstants.RN_IS_AUTO_START)) {
                isAutoStart = readableMap.getBoolean(PolyvRNConstants.RN_IS_AUTO_START);
            }
//            String vid = readableMap.getString(PolyvRNConstants.RN_VID);
//            player.play(vid, 1, isAutoStart, false);
            if (readableMap.hasKey(PolyvRNConstants.RN_SHOW_CONTROL)) {
                showControl = readableMap.getBoolean(PolyvRNConstants.RN_SHOW_CONTROL);
            }

            if (readableMap.hasKey(PolyvRNConstants.RN_LOOP_PLAY)) {
                loopPlay = readableMap.getBoolean(PolyvRNConstants.RN_LOOP_PLAY);
            }
            System.out.println(TAG + "setPlayerLoopPlay" + loopPlay);
            player.setPlayerLoopPlay(loopPlay);
            System.out.println(TAG + "player.initPlay");
            player.initPlay(1, isAutoStart, false, showControl, new IPolyvRNMediaController() {
                @Override
                public void changeToPortrait() {
                    System.out.println(TAG + "changeToPortrait");
                    onReceiveNativeVodEvent(player, "fullscreen", false);
                }

                @Override
                public void changeToLandscape() {
                    System.out.println(TAG + "changeToLandscape");
                    onReceiveNativeVodEvent(player, "fullscreen", true);
                }

                @Override
                public void goBack() {
                    onReceiveNativeVodEvent(player, "goBack", true);
                }
            });


            if (readableMap.hasKey(PolyvRNConstants.RN_FULLSCREEN)) {
                isFullScreen = readableMap.getBoolean(PolyvRNConstants.RN_FULLSCREEN);
            }
            player.setPlayerFullScreen(isFullScreen);

            if (readableMap.hasKey(PolyvRNConstants.RN_IS_CAN_DRAG)) {
                isCanDrag = readableMap.getBoolean(PolyvRNConstants.RN_IS_CAN_DRAG);
            }
            player.setCanDrag(isCanDrag);
            if (readableMap.hasKey(PolyvRNConstants.RN_MARQUEE)) {
                ReadableMap marquee = readableMap.getMap(RN_MARQUEE);
                player.playMarquee(marquee);
            }
        } catch (Exception e) {
            PolyvCommonLog.exception(e);
        }
    }

}
